package com.example.transformpage.View;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.baselibrary.libs.Utils.log.LogUtils;
import com.baselibrary.libs.Utils.windowSize.UtilsWindowSize;
import com.baselibrary.libs.View.baseView.BaseView;
import com.example.transformpage.R;
import com.example.transformpage.ViewPaperAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator 于萌萌
 * 创建日期: 17:25 . 2017年02月15日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class GalleryView  extends BaseView{

    private ViewPager viewPager;
    private ViewPaperAdapter vpAdapter;
    private float MIN_SCALE;
    public GalleryView(Context context) {
        super(context);
    }

    public GalleryView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GalleryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public int getXml() {
        return R.layout.gallery_view;
    }

    @Override
    public void initView(View view) {
        viewPager = (ViewPager)this.findViewById(R.id.viewpager);


    }

    /**
     * 设定幅度
     * @param i
     * @return
     */
    public GalleryView setScale(int i){
        MIN_SCALE = (float)i/100;
        return this;
    }


    /**设置缩放比
     *
     * @param activity
     * @param size 范围 1 - 100
     * @return
     */
    public GalleryView setSize(Activity activity,int size){
        //设置缩放比
        UtilsWindowSize.setSize(activity, viewPager,size);
        return this;
    }

    /**
     * 设定数据
     * @param context
     * @param views
     * @return
     */
    public GalleryView setData(Context context, ArrayList<RelativeLayout> views){


        vpAdapter = new ViewPaperAdapter(views);
        viewPager.setOffscreenPageLimit(views.size());
        viewPager.setAdapter(vpAdapter);
        //viewPager.setPageMargin(-90);
        setPageTransformers(context);



        return this;
    }
    viewPagerCallBack mViewPagerCallBack;
    public  interface  viewPagerCallBack{
        void callItem(int postion);

    }
    public void setViewPagerCallBack(viewPagerCallBack mViewPagerCallBack){
        this.mViewPagerCallBack = mViewPagerCallBack;


    }

    public void viewPageritem(viewPagerCallBack mViewPagerCallBack){

    }


    private void setPageTransformers(Context context) {
        viewPager.setPageMargin(dip2px(context, 70)*-1);

        viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View view, float position) {
                LogUtils.i("滑动的position\t"+position);
                if(position < -1 || position > 1){
                    view.setScaleX(1);
                    view.setScaleY(1);
                    return ;
                }

                if (MIN_SCALE == 0){
                    MIN_SCALE = (float)85/100;
                }
                float changeArea = 1 - MIN_SCALE;
                position = Math.abs(position);
                float scaleFactor = position * changeArea;
                view.setScaleX(1 - scaleFactor);
                view.setScaleY(1 - scaleFactor);

            }
        });
    }

    public  int dip2px(Context context, float dipValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }
}
